package com.ruoyi.web.controller.huashu;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Banner;
import com.ruoyi.system.domain.BannerSelectListDto;
import com.ruoyi.system.domain.BannerUpdateDto;
import com.ruoyi.system.service.IBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.rmi.ServerException;
import java.util.List;

/**
 * bannerController
 *
 * @author 杨杰
 * @date 2024-03-04
 */
@Api(tags = {"（华数）banner管理"})
@Controller
@RequestMapping("TV/system/banner")
public class HuashuBannerController extends BaseController {
    private String prefix = "system/banner";

    @Autowired
    private IBannerService bannerService;

    @GetMapping()
    public String banner() {
        return prefix + "/banner";
    }

    /**
     * 查询banner列表
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value = "banner列表", response = Banner.class)

    public TableDataInfo list(BannerSelectListDto bannerList) {
        startPage();
        Banner banner = new Banner();
        BeanUtil.copyProperties(bannerList, banner);
        List<Banner> list = bannerService.selectBannerList(banner);
        return getDataTable(list);
    }

    @PostMapping("/typeList")
    @ResponseBody
    @ApiOperation(value = "banner类型查询接口", response = Banner.class)
    public AjaxResult<List<Banner>> typeList(BannerSelectListDto bannerList) {
//        startPage();
        Banner banner = new Banner();
        BeanUtil.copyProperties(bannerList, banner);
        List<Banner> list = bannerService.selectBannerList(banner);
        return AjaxResult.success(list);
    }


    /**
     * 导出banner列表
     */
    @Log(title = "banner", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody

    public AjaxResult export(Banner banner) {
        List<Banner> list = bannerService.selectBannerList(banner);
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        return util.exportExcel(list, "banner数据");
    }

    /**
     * 新增banner
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存banner
     */
    @Log(title = "banner", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加banner")
    public AjaxResult addSave(@RequestBody BannerSelectListDto addDto) throws ServerException {
        Banner banner = new Banner();
        BeanUtil.copyProperties(addDto, banner);
        return toAjax(bannerService.insertBanner(banner));
    }

    /**
     * 修改banner
     */
    @GetMapping("/edit/{bannerId}")
    public String edit(@PathVariable("bannerId") Long bannerId, ModelMap mmap) {
        Banner banner = bannerService.selectBannerByBannerId(bannerId);
        mmap.put("banner", banner);
        return prefix + "/edit";
    }

    /**
     * 修改保存banner
     */
    @Log(title = "banner", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "修改banner")
    public AjaxResult editSave(BannerUpdateDto updateDto) throws ServerException {
        Banner banner = new Banner();
        BeanUtil.copyProperties(updateDto, banner);
        return toAjax(bannerService.updateBanner(banner));
    }

    /**
     * 删除banner
     */
    @Log(title = "banner", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    @ApiOperation(value = "删除banner")
    public AjaxResult remove(String ids) {
        return toAjax(bannerService.deleteBannerByBannerIds(ids));
    }
}
